﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TinyERP.Framework.Controls;
using TinyERP.Framework;
using TinyERP.Core.DTO;

namespace TinyERP.Core.View.Controls
{
    public class KApplicationRibbon : Ribbon, ILocalizable, INavigator
    {
        #region ILocalizable Members

        public void UpdateLanguage()
        {
            foreach (RibbonTab tab in this.Tabs)
            {
                if (tab is KRibbonTab)
                {
                    ((KRibbonTab)tab).UpdateLanguage();
                }
            }
        }

        #endregion

        #region INavigator Members
        private RibbonItem ribbonItemContainsUINode;
        public event SelectNodeHandler OnSelectedNode;
        private RibbonTab FindTabContainsFormNode(UINodeDTO formNode)
        {
            for (int i = 1; i < this.Tabs.Count; i++)
            {
                RibbonTab tab = this.Tabs[i];
                if (IsTabContainsNode(tab, formNode))
                {
                    return tab;
                }
            }
            return null;
        }
        private bool IsPanelContainsNode(RibbonPanel panel, UINodeDTO formNode)
        {
            foreach (RibbonItem item in panel.Items)
            {
                if (IsRibbonItemContainsNode(item, formNode))
                {
                    return true;
                }
            }
            return false;
        }
        private void ClearAllCheckedItems()
        {
            for (int i = 1; i < this.Tabs.Count; i++)
            {
                RibbonTab tab = this.Tabs[i];
                ClearCheckedOnTab(tab);
            }
        }
        private void ClearCheckedOnTab(RibbonTab tab)
        {
            foreach (RibbonPanel panel in tab.Panels)
            {
                ClearCheckedOnPanel(panel);
            }
        }
        private void ClearCheckedOnPanel(RibbonPanel panel)
        {
            foreach (RibbonItem item in panel.Items)
            {
                ClearCheckedOnItem(item);
            }
        }

        private void ClearCheckedOnItem(RibbonItem ribbonItem)
        {
            ribbonItem.Checked = false;
            if (ribbonItem is RibbonButton)
            {
                RibbonItemCollection childItemCollection = ((RibbonButton)ribbonItem).DropDownItems;
                foreach (RibbonItem childItem in childItemCollection)
                {
                    ClearCheckedOnItem(childItem);
                }
            }
        }
        private bool IsRibbonItemContainsNode(RibbonItem ribbonItem, UINodeDTO formNode)
        {
          
            if (ribbonItem.Tag is UINodeDTO && (UINodeDTO)ribbonItem.Tag == formNode)
            {
                ribbonItemContainsUINode = ribbonItem;
                return true;
            }
            if (ribbonItem is RibbonButton)
            {
                RibbonItemCollection childItemCollection = ((RibbonButton)ribbonItem).DropDownItems;
                foreach (RibbonItem childItem in childItemCollection)
                {
                    if (IsRibbonItemContainsNode(childItem, formNode))
                    {
                        ribbonItemContainsUINode = childItem;
                        return true;
                    }
                }
            }
            return false;
        }
        private bool IsTabContainsNode(RibbonTab tab, UINodeDTO formNode)
        {
            if (tab.Tag is UINodeDTO)
            {
                if ((UINodeDTO)tab.Tag == formNode)
                {
                    return true;
                }
            }
            foreach (RibbonPanel panel in tab.Panels)
            {
                if (IsPanelContainsNode(panel, formNode))
                {
                    return true;
                }
            }
            return false;
        }
        public void SelectNode(TinyERP.Core.DTO.UINodeDTO formNode)
        {
            ribbonItemContainsUINode = null;
            ClearAllCheckedItems();
            RibbonTab tabContainsNode = FindTabContainsFormNode(formNode);
            if (tabContainsNode != null)
            {
                this.ActiveTab = tabContainsNode;
                if (this.ribbonItemContainsUINode != null)
                {
                    this.ribbonItemContainsUINode.Checked = true;
                }
            }
        }

        public void AddNode(TinyERP.Core.DTO.UINodeDTO formNode)
        {
            AddRibbonTab(formNode);
        }

        public void ClearAllNodes()
        {
            KRibbonTab firstTab = (KRibbonTab)this.Tabs[0];
            this.Tabs.Clear();
            this.Tabs.Add(firstTab);
        }
        public void BuildNavigator()
        {
            KRibbonTab firstTab = (KRibbonTab)this.Tabs[0];
            this.Tabs.Clear();
            this.Tabs.Add(firstTab);
            for (int i = Session.ApplicationNodes.Count - 1; i >= 0; i--)
            {
                UINodeDTO node = (UINodeDTO)Session.ApplicationNodes[i];
                AddRibbonTab(node);
            }
        }
        #endregion

        
        
        private void AddRibbonTab(UINodeDTO node)
        {
            KRibbonTab ribbonTab = new KRibbonTab();
            ribbonTab.Text = UIManager.GetInstance().GetTitleOfUINode(node);
            RibbonPanel panel = null;
            if (node.Children != null)
            {
                for (int i = 0; i < node.Children.Count; i++)
                {
                    if (i % 3 == 0)
                    {
                        panel = new RibbonPanel();
                        panel.FlowsTo = RibbonPanelFlowDirection.Bottom;
                        if (!string.IsNullOrEmpty(node.FlowTo) && node.FlowTo.ToLower() == "right")
                        {
                            panel.FlowsTo = RibbonPanelFlowDirection.Right;
                        }
                        else
                        {
                            panel.FlowsTo = RibbonPanelFlowDirection.Bottom;
                        }
                        panel.ButtonMoreVisible = false;
                        ribbonTab.Panels.Add(panel);
                    }
                    UINodeDTO childNode = (UINodeDTO)node.Children[i];
                    AddRibbonButton(childNode, panel);
                }
            }
            this.Tabs.Add(ribbonTab);
        }
        private void AddRibbonButton(UINodeDTO node, RibbonPanel panel)
        {
            KRibbonButton button = new KRibbonButton();
            button.ResourceModuleKey = node.ResourceKey;
            button.Tag = node;
            button.Text = UIManager.GetInstance().GetTitleOfUINode(node);
            if (!string.IsNullOrEmpty(node.IconKey))
            {
                button.Image = ModuleResourceManager.GetInstance().GetImageFromModule(node.IconKey);
                button.SmallImage = ModuleResourceManager.GetInstance().GetImageFromModule(node.IconKey);
            }
            button.MaxSizeMode = RibbonElementSizeMode.Medium;
            button.Click += new EventHandler(RibbonButtonClick);
            if (node.Children != null && node.Children.Count > 0)
            {
                foreach (UINodeDTO childNode in node.Children)
                {
                    AddRibbonButton(childNode, button);
                }
            }
            panel.Items.Add(button);
        }

        private void AddRibbonButton(UINodeDTO node, RibbonButton ribbonButton) 
        {
            KRibbonButton button = new KRibbonButton();
            button.ResourceModuleKey = node.ResourceKey;
            button.Tag = node;
            button.Text = UIManager.GetInstance().GetTitleOfUINode(node);
            button.Click += new EventHandler(RibbonButtonClick);
            ribbonButton.DropDownItems.Add(button);
            if (node.Children != null && node.Children.Count > 0)
            {
                foreach (UINodeDTO childNode in node.Children)
                {
                    AddRibbonButton(childNode, button);
                }
            }
        }
        private void RibbonButtonClick(object sender, EventArgs e)
        {
            if (sender is RibbonButton)
            {
                UINodeDTO selectedNode = (UINodeDTO)((RibbonButton)sender).Tag;
                if (OnSelectedNode != null)
                {
                    OnSelectedNode(this, selectedNode);
                }
            }
        }
    }
}
